package de.test.hello;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class HelloWorldActivity extends Activity implements OnClickListener {
	private Button hiButton;
	private Button helloButton;
	private String greeting = "no greeting saved";
	private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;


	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        hiButton = (Button)findViewById(R.id.hi_button);
        hiButton.setOnClickListener(this);
        helloButton = (Button)findViewById(R.id.hello_button);
        helloButton.setOnClickListener(this);
        Button btnFinanzen = (Button)findViewById(R.id.btnFinanzen);
        btnFinanzen.setOnClickListener(new View.OnClickListener(){
            	public void onClick(View v) {
            		Intent finanzuebersicht = new Intent(HelloWorldActivity.this,Finanzuebersicht.class);
            		finanzuebersicht.putExtra("value1", greeting);
            		startActivity(finanzuebersicht);
            		}
    });
  }
 /*       
     // Gesture detection
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };
}
*/
    public void onClick(View v) {
		EditText nameField = (EditText) findViewById(R.id.name_field);
		String name = nameField.getText().toString();
		if (name.length() == 0) {
		    new AlertDialog.Builder(this) 
		            .setMessage(R.string.error_name_missing)
		            .setNeutralButton(R.string.error_ok, null)
		            .show();
		    return;
		}	
		   if (v == hiButton || v == helloButton) {
	            int resourceId = v == hiButton ? R.string.hi_greeting
	                    : R.string.hello_greeting;

	            greeting = getResources().getString(resourceId, name);
	            Toast.makeText(this, greeting, Toast.LENGTH_LONG).show();

	            TextView greetingField = (TextView) findViewById(R.id.greeting_field);
	            greetingField.setText(greeting);
	        }
	
	
	}
    
    public boolean onCreateOptionsMenu(Menu optMenu){
    	optMenu.add(1, 1, 0, "Einstellungen").setIcon(R.drawable.properties);
    	optMenu.add(1, 2, 1, "Kategorien").setIcon(R.drawable.ic_launcher);
    	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()){
    	case 1:
    		Intent properties = new Intent(HelloWorldActivity.this,Properties.class);
    		startActivity(properties);
    		return true;
    	case 2:
    		Intent finanzuebersicht = new Intent(HelloWorldActivity.this,Finanzuebersicht.class);
    		finanzuebersicht.putExtra("value1", greeting);
    		startActivity(finanzuebersicht);
    	default: 
    		return super.onOptionsItemSelected(item);
    	}
    }

  /*  
    class MyGestureDetector extends SimpleOnGestureListener implements OnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(HelloWorldActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(HelloWorldActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

		public boolean onDown(MotionEvent arg0) {
			// TODO Auto-generated method stub
			return false;
		}

		public void onLongPress(MotionEvent e) {
			// TODO Auto-generated method stub
			
		}

		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			// TODO Auto-generated method stub
			return false;
		}

		public void onShowPress(MotionEvent e) {
			// TODO Auto-generated method stub
			
		}

		public boolean onSingleTapUp(MotionEvent e) {
			// TODO Auto-generated method stub
			return false;
		}


}
**/
}